package com.atguigu.multiReading;
class print{
    public static int flag=3;
    public synchronized void printA(){
        while(flag!=1){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
            System.out.println(Thread.currentThread().getName());
            flag=3;
            notifyAll();

    }
    public synchronized void printB(){
        while(flag!=2){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
            System.out.println(Thread.currentThread().getName());
            flag=1;
            notifyAll();

    }
    public synchronized void printC(){
        while(flag!=3){
            try{
                wait();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName());
        flag=2;
        notifyAll();
    }
}
public class demo8 extends Thread{
    public static void main(String[] args) {
        demo8 t1=new demo8();
        t1.setName("a");
        demo8 t2=new demo8();
        t2.setName("b");
        demo8 t3=new demo8();
        t3.setName("c");
        t1.start();
        t2.start();
        t3.start();
    }

    public void run(){
        print p=new print();
        while(true) {
            if (Thread.currentThread().getName().equals("a")) {
                p.printA();
            } else if (Thread.currentThread().getName().equals("b")) {
                p.printB();
            } else if (Thread.currentThread().getName().equals("c")) {
                p.printC();
            }
        }
    }
}
